#include "StdAfx.h"
#include "UICaptionButton.h"
#include "UIImgGdiplus.h"
#include "UIClip.h"

CUICaptionButton::CUICaptionButton( HWND hWnd, UINT uID, UINT uImgID
                                    , IMGDIRECTION imgDir, UINT uImgCount, LRESULT hitCode ): m_pImg( NULL )
    , m_iCurStatus( IMG_NORMAL ), m_bPress( FALSE ), m_bHover( FALSE ), m_iHitCode( hitCode )
{
    m_hParent = hWnd;
    m_CtrlID = uID;
    GetImg()->LoadImg( uImgID, imgDir, uImgCount );
}

CUICaptionButton::CUICaptionButton( HWND hWnd, UINT uID, LPCTSTR lpszImg
                                    , IMGDIRECTION imgDir, UINT uImgCount, LRESULT hitCode ): m_pImg( NULL )
    , m_iCurStatus( IMG_NORMAL ), m_bPress( FALSE ), m_bHover( FALSE ), m_iHitCode( hitCode )
{
    m_hParent = hWnd;
    m_CtrlID = uID;
    GetImg()->LoadImg( lpszImg, imgDir, uImgCount );
}

CUICaptionButton::~CUICaptionButton( void )
{
    if ( NULL != m_pImg )
    {
        delete m_pImg;
        m_pImg = NULL;
    }
}

CUIImgGdiplus* CUICaptionButton::GetImg()
{
    if ( NULL == m_pImg )
    {
        m_pImg = new CUIImgGdiplus();
    }

    return m_pImg;
}

void CUICaptionButton::Draw( HDC hDC )
{
//	CUIClip clip(hDC);

// 	int iLeft=GetImg()->GetPosition().x;
// 	int iTop=GetImg()->GetPosition().y;
// 	int iRight=iLeft+GetImg()->GetSize().cx;
// 	int iBottom=iTop+GetImg()->GetSize().cy;
// 	HRGN hOldRgn1=::CreateRectRgn(iLeft,iTop,iRight,iBottom);
// 	::SelectClipRgn(hDC,hOldRgn1);

    if ( m_bHover )
    {
        GetImg()->SetCurStatus( IMG_HOVER );
        GetImg()->Draw( hDC );

        if ( m_bPress )
        {
            GetImg()->SetCurStatus( IMG_PRESS );
            GetImg()->Draw( hDC );
        }
    }
    else
    {
        if ( m_bPress )
        {
            GetImg()->SetCurStatus( IMG_PRESS );
            GetImg()->Draw( hDC );
        }
        else
        {
            GetImg()->SetCurStatus( IMG_NORMAL );
            GetImg()->Draw( hDC );
        }
    }

//	::DeleteObject(hOldRgn1);

// 	RECT rcPaint;
// 	::SetRect(&rcPaint,GetImg()->GetPosition().x,GetImg()->GetPosition().y,GetImg()->GetSize().cx,GetImg()->GetSize().cy);
// 	clip.AddRect(rcPaint);
}

const RECT CUICaptionButton::GetBorder()
{
    RECT rc;
    rc.left = GetImg()->GetPosition().x;
    rc.top = GetImg()->GetPosition().y;
    rc.right = rc.left + GetImg()->GetSize().cx;
    rc.bottom = rc.top + GetImg()->GetSize().cy;
    return rc;
}

SIZE CUICaptionButton::GetSize()
{
    return GetImg()->GetSize();
}

UINT CUICaptionButton::GetID()const
{
    return m_CtrlID;
}

void CUICaptionButton::SetPosition( int x, int y )
{
    GetImg()->SetPosition( x, y );
}

LRESULT CUICaptionButton::GetHitCode()const
{
    return m_iHitCode;
}

void CUICaptionButton::SetPressed( BOOL bPress )
{
    m_bPress = bPress;
}

void CUICaptionButton::SetHovered( BOOL bHover )
{
    m_bHover = bHover;
}

BOOL CUICaptionButton::IsPressed()const
{
    return m_bPress;
}

BOOL CUICaptionButton::IsHovered()const
{
    return m_bHover;
}